﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVT.DAO;
using QLVT.Entity;

namespace QLVT.UI.UserControlUI
{
    public partial class UcKho : UserControl
    {
        bool add = false;
        public UcKho()
        {
            InitializeComponent();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            String makho = txtMaKho.Text;
            if (makho.Length != 0)
            {
                KhoDAO khoDAO = new KhoDAO();
                if (!khoDAO.DeleteBymakho(makho))
                {
                    MessageBox.Show("Không xóa được kho", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = khoDAO.GetAll();
                    dataGridViewKho.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(khoDAO.GetBymakho(dataGridViewKho.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            Kho kho = getData();
            if (kho != null)
            {
                KhoDAO khoDAO = new KhoDAO();
                if (!khoDAO.UpdateBymakho(kho))
                {
                    MessageBox.Show("Không cập nhật được kho", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = khoDAO.GetAll();
                    dataGridViewKho.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(khoDAO.GetBymakho(dataGridViewKho.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            if (!add)
            {
                toolStripButtonOk.Visible = true;
                add = true;
                toolStripButtonAdd.Visible = false;
                txtMaKho.Text = null;
                txtTenKho.Text = null;
            }
        }

        private void toolStripButtonOk_Click(object sender, EventArgs e)
        {
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
                Kho kho = getData();
                if (kho != null)
                {
                    KhoDAO khoDAO = new KhoDAO();
                    if (!khoDAO.Insert(kho))
                    {
                        MessageBox.Show("Không tạo thêm được kho", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DataSet ds = khoDAO.GetAll();
                        dataGridViewKho.DataSource = ds.Tables[0];
                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            BinddingData(khoDAO.GetBymakho(dataGridViewKho.Rows[0].Cells[0].Value.ToString()));
                        }
                    }
                }
            }
        }

        private void dataGridViewKho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow rowSelect = dataGridViewKho.SelectedRows[0];
            KhoDAO khoDao = new KhoDAO();
            Kho nhanvien = khoDao.GetBymakho(rowSelect.Cells[0].Value.ToString());
            if (nhanvien != null)
            {
                BinddingData(nhanvien);
            }
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
            }
        }

        private void UcKho_Load(object sender, EventArgs e)
        {
            KhoDAO khoDAO = new KhoDAO();
            DataSet ds = khoDAO.GetAll();
            DataGridViewTextBoxColumn columnMaKho = new DataGridViewTextBoxColumn();
            columnMaKho.HeaderText = "Mã kho";
            columnMaKho.DataPropertyName = "makho";
            dataGridViewKho.Columns.Add(columnMaKho);
            DataGridViewTextBoxColumn columnTen = new DataGridViewTextBoxColumn();
            columnTen.HeaderText = "Tên kho";
            columnTen.DataPropertyName = "tenkho";
            dataGridViewKho.Columns.Add(columnTen);
            dataGridViewKho.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewKho.DataSource = ds.Tables[0];
        }

        public Kho getData()
        {
            Kho kho = null;
            try
            {
                kho = new Kho();
                kho.Makho = txtMaKho.Text;
                kho.Tenkho = txtTenKho.Text;
                return kho;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void BinddingData(Kho kho)
        {
            txtMaKho.Text = kho.Makho;
            txtTenKho.Text = kho.Tenkho;
        }
    }
}
